package com.liaowei.novel.fragment;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.liaowei.novel.R;
import com.liaowei.novel.activity.HistoryActivity;
import com.liaowei.novel.domain.History;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SQLiteDatabase db;
    private Cursor cursor;
    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;

    public MineFragment() {
        // Required empty public constructor
    }
    public MineFragment(SQLiteDatabase db) {
        this.db = db;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        TableRow exitBtn = view.findViewById(R.id.exitBtn);
        TableRow browseBtn = view.findViewById(R.id.browseBtn);
        TableRow openSourceBtn = view.findViewById(R.id.openSourceBtn);
        exitBtn.setOnClickListener(this);
        browseBtn.setOnClickListener(this);
        openSourceBtn.setOnClickListener(this);

        return view;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.exitBtn:
                getActivity().finish();
                break;
            case R.id.browseBtn:
                Intent intent = new Intent(getActivity(), HistoryActivity.class);
                startActivity(intent);
                break;
            case R.id.openSourceBtn:
                alert = null;
                builder = new AlertDialog.Builder(getContext());
                alert = builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("开源地址")
                        .setMessage("如果喜欢，可以star一下\nhttps://gitee.com/JohnSnow2206/weiy-novel")
                        .setNegativeButton("复制", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 复制地址
                                ClipboardManager manager = (ClipboardManager) getContext().getSystemService(getContext().CLIPBOARD_SERVICE);
                                ClipData mClipData = ClipData.newRawUri("openSource", Uri.parse("https://gitee.com/JohnSnow2206/weiy-novel"));
                                manager.setPrimaryClip(mClipData);

                                Toast.makeText(getContext(), "复制成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 不做任何事
                            }
                        }).create();
                alert.show(); //显示对话框
                break;
        }
    }
}